<template>
  <div class="app-container bg-gray">
    <el-card class="box-1">
      <div class="header">
        <div>
          <el-select v-model="searchTerms.type" placeholder="试卷分类" class="select" clearable @change="getData">
            <el-option v-for="item in typeList" :key="item.id" :value="item.id" :label="item.name"/>
          </el-select>
          <el-input v-model="searchTerms.searchValue" placeholder="请输入考试名称" class="search-input" @change="getData"/>
          <el-button type="success" class="do-btn" icon="el-icon-search" @click="getData">搜索</el-button>
        </div>
        <div class="right-btns">
          <router-link to="/exam/examPaperType">
            <el-button v-if="queryMatch(198)" type="primary">分类管理</el-button>
          </router-link>
          <el-button v-if="queryMatch(194)" type="primary" @click="toCreateExamPaper">创建试卷</el-button>
          <el-button v-if="queryMatch(195)" type="warning" @click="toExport">导出数据</el-button>
          <!--<el-button type="danger" @click="toBatchDelete">批量删除</el-button>-->
        </div>
      </div>
      <el-table
        v-loading="tableLoading"
        ref="multipleTable"
        :data="tableData"
        :max-height="tableMaxHeight"
        border
        highlight-current-row
        @sort-change="handleSortChange"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="id" label="ID" width="100" sortable/>
        <el-table-column prop="name" label="试卷名称" min-width="140"/>
        <el-table-column prop="type_name" label="分类" width="120" header-align="center" align="center"/>
        <el-table-column prop="single" label="单选题数" width="120" header-align="center" align="center"/>
        <el-table-column prop="many" label="多选题数" width="120" header-align="center" align="center"/>
        <el-table-column prop="fill" label="填空题数" width="120" header-align="center" align="center"/>
        <el-table-column label="总分" width="140" header-align="center" align="center">
          <template slot-scope="scope">
            <el-tag type="info">{{ scope.row['raction'] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="220" header-align="center" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button type="info" class="bg-green" size="small" icon="el-icon-edit" circle @click="toModifyExam(scope.row)"/>
            <el-tooltip content="题目" placement="top">
              <router-link :to="`/exam/examPaperQuestions?id=${scope['row']['id']}`">
                <el-button v-if="queryMatch(197)" type="primary" size="small" icon="el-icon-more" circle/>
              </router-link>
            </el-tooltip>
            <el-button type="danger" size="small" icon="el-icon-delete" circle @click="toDelete(scope.row)"/>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page.sync="currentPage"
        :page-size="pageSize"
        :page-sizes="pageSizes"
        :total="total"
        class="pagination"
        layout="total, sizes, prev, pager, next, jumper"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"/>
    </el-card>

    <el-dialog :visible.sync="dialogVisible" :title="dialogTitle" width="500px" class="dialog" center>
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="试卷名称" prop="name">
          <el-input v-model="form.name"/>
        </el-form-item>
        <el-form-item label="试卷类型" prop="type">
          <el-select v-model="form.typeId" placeholder="请选择分类" filterable>
            <el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { handleSortChange, handleSizeChange, handleCurrentChange, parseTime } from '@/utils/table'

export default {
  data() {
    return {
      searchTerms: {
        type: '',
        date: [],
        searchValue: ''
      },

      // 类型数组
      typeList: [],

      dialogVisible: false,
      dialogTitle: '创建试卷',
      form: {
        id: '',
        name: '',
        typeId: 0
      },

      // 表格相关数据
      tableMaxHeight: document.documentElement.clientHeight - 380,
      tableLoading: false,
      currentPage: 1,
      pageSize: this.$store.getters.userInfo.pageSize,
      pageSizes: [10, 20, 50, 100, 1000],
      total: 0,
      tableData: [],
      multipleSelection: []
    }
  },
  created() {
    this.getData();
    this.getTypeList();
  },
  methods: {
    handleCurrentChange, handleSizeChange, handleSortChange, parseTime,
    getData(order) {
      this.tableLoading = true;
      order || (order = {});
      const url = 'exam/testPaperList';
      const data = {
        pageNo: this.currentPage,
        pageSize: this.pageSize,
        typeId: this.searchTerms.type,
        search: this.searchTerms.searchValue
      };
      order.sortName && (data.sortName = order.sortName);
      order.sortOrder && (data.sortOrder = order.sortOrder);
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        this.tableData = res.data.rows;
        this.total = res.data.total;
        this.tableLoading = false;
      }).catch(e => {
        this.tableLoading = false;
        this.$message.error(e.msg + ',请刷新或联系管理员');
      })
    },

    // 获取分类
    getTypeList() {
      const url = '/exam/typeList'
      this.$store.dispatch('GetConnect', { url }).then(res => {
        this.typeList = res.data
      }).catch(e => {
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    // table多选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    toCreateExamPaper() {
      this.form = {};
      this.dialogVisible = true;
    },

    toModifyExam(row) {
      Object.assign(this.form, row);
      this.dialogTitle = '编辑试卷';
      this.dialogVisible = true;
    },

    // 弹窗提交
    submit() {
      this.dialogVisible = false;
      const data = this.form;
      const url = data.id ? 'exam/updateTestPaper' : 'exam/addTestPaper';
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        this.$message.success(res.msg)
        this.dialogVisible = false
        this.getData();
        this.$refs.form.resetFields();
      }).catch((err) => {
        this.$message.error(err.msg)
      })
    },

    toDelete(row) {
      this.$confirm('此操作将删除所选试卷, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const url = 'exam/delTestPaper/' + row.id
        this.$store.dispatch('GetConnect', { url }).then(res => {
          this.$message.success(res.msg)
          this.getData()
        }).catch((err) => {
          this.$message.error(err.msg)
        })
      })
    },

    toMultipleDelete() {
      if (this.multipleSelection.length === 0) {
        this.$message.error('请选择要删除的试卷');
        this.dialogVisible = false;
      } else {
        this.$confirm('此操作将删除所选考生, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // const url = 'exam/delTestPaper/'
          const data = {
            id: []
          };
          this.multipleSelection.forEach(item => {
            data.id.push(item.id)
          });
          data.id = data.id.join(',');
          // this.$store.dispatch('GetConnect', { url, data }).then(res => {
          //   this.$message.success(res.msg)
          //   this.getData()
          // }).catch((err) => {
          //   this.$message.error(err.msg)
          // })
        })
      }
    },

    // 批量导出试卷
    toExport(){
      if (this.multipleSelection.length === 0) {
        this.$message.error('请选择要导出的试卷');
        this.dialogVisible = false
      } else {
        const data = {
          ids: []
        }
        this.multipleSelection.forEach(item => {
          data.ids.push(item.id)
        })
        data.ids = data.ids.join(',')
        window.open(this.$store.getters.serverUrl + '/admin/exam/testPaperToExcel?token=' + this.$store.getters.token + '&ids=' + data.ids + '&mallId=' + this.$store.getters.SaaSId);
      }
    }
  }
}
</script>

<style lang="scss" type="text/scss" scoped>
@import '../../../../styles/index.scss';
.box-1 {
  .model {
    .el-card {
      color: #fff;
      background-color: $panGreen;
      span {
        font-size: 14px;
      }
      div {
        font-size: 24px;
        margin-top: 10px;
      }
    }
  }
  .header {
    @include flex-row(center, space-between);
    flex-wrap: wrap;
    padding-bottom: 15px;
    div {
      @include flex-row(center, flex-start);
      flex-wrap: wrap;
      .select {
        padding-right: 15px;
        width: 150px;
      }
      .picker {
        margin-right: 15px;
        width: 300px;
      }
      .do-btn {
        @extend .bg-green;
        margin-left: 6px;
      }
      .search-input {
        width: 180px;
      }
    }
    .right-btns{
      .el-button{
        margin-left: 10px;
      }
    }
  }
}
.dialog {
  .content {
    text-align: center;
    .el-select {
      width: 300px;
    }
    .text {
      padding-bottom: 20px;
      .el-radio-group {
        padding-left: 15px;
      }
    }
  }
}
</style>

